package o2o.web.actions.order;

import com.jinyou.utils.common.ValidateUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// 单利模式
// 验证码库
public class VerifyCodeMan {

    private VerifyCodeMan() {
    }

    private static VerifyCodeMan instance = new VerifyCodeMan();

    public static VerifyCodeMan getInstance() {
        if (instance == null) {
            instance = new VerifyCodeMan();
        }
        return instance;
    }

    private static List<String> verifyCodeList = null;

    public void run() {
        if (!ValidateUtil.isAbsList(verifyCodeList)) {
            // 获取验证码 并且获取到中；
            verifyCodeList = initList();
            Collections.shuffle(verifyCodeList);
        }
    }

    // 生成1000-9999的小数
    private synchronized List<String> initList() {
        List<String> list = new ArrayList<>();
        for (int i = 1000; i < 10000; i++) {
            list.add(i + "");
        }
        return list;
    }

    // 获取code
    public List getVerifyCodeList() {
        if (!ValidateUtil.isAbsList(verifyCodeList)) {
            verifyCodeList = initList();
        }
        return verifyCodeList;
    }

}